package com.med.service.impl;

import com.med.common.ResponseDTO;
import com.med.entity.DoctorDO;
import com.med.entity.ScheduleDO;
import com.med.enums.ResponseCode;
import com.med.mapper.NotificationMapper;
import com.med.mapper.SchedulingMapper;
import com.med.service.ScheduleService;
import com.med.service.SchedulingService;
import com.med.vo.AddSchedulingVO;
import com.med.vo.QueryWorkforceVO;
import com.med.vo.UpdateSchedulingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * @BelongsProject: Internet-based-healthcare
 * @BelongsPackage: com.med.service.impl
 * @Author: 小龚
 * @CreateTime: 2025-08-19 10:28
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class SchedulingServiceImpl implements SchedulingService {

    private final SchedulingMapper schedulingMapper;

    private final ScheduleService scheduleService;

    private final NotificationMapper notificationMapper;


    @Autowired
    public SchedulingServiceImpl(SchedulingMapper schedulingMapper, ScheduleService scheduleService, NotificationMapper notificationMapper) {
        this.schedulingMapper = schedulingMapper;
        this.scheduleService = scheduleService;
        this.notificationMapper = notificationMapper;
    }

    // 查询排班信息
    @Override
    public List<ScheduleDO> getWorkforceList(QueryWorkforceVO vo) {
        List<String> dateRange = vo.getDateRange();
        // 开始时间
        LocalDate startDate;
        // 结束时间
        LocalDate endDate;
        if (dateRange == null) {
            startDate = null;
            endDate = null;
        } else {
            startDate = LocalDate.parse(dateRange.get(0));
            endDate = LocalDate.parse(dateRange.get(1));
        }
        System.out.println("开始时间" + startDate);
        System.out.println("结束时间" + endDate);
        // 获取医生科室
        List<Integer> ids = vo.getDoctorDepartmentId();
        Integer departmentId;
        if (ids == null) {
            departmentId = null;
        } else {
            departmentId = ids.get(1);
        }
        System.out.println("部门id" + departmentId);
        List<ScheduleDO> workforceList = schedulingMapper.getWorkforceList(vo.getDoctorName(), departmentId, startDate, endDate);
        return workforceList;
    }


    // 修改排班状态
    @Override
    public ResponseDTO updateWorkforceStatus(Integer scheduleId) {
        Integer doctorId = schedulingMapper.getDoctorByScheduleId(scheduleId);
        ScheduleDO scheduleDO = schedulingMapper.checkScheduling(doctorId);
        if (scheduleDO != null) {
            return ResponseDTO.error(ResponseCode.ERROR, "当前排班已被预约，无法禁用");
        }
        Integer count = schedulingMapper.updateWorkforceStatus(scheduleId);
        if (count > 0) {
            return ResponseDTO.success();
        }
        return ResponseDTO.error(ResponseCode.ERROR);
    }


    // 删除排班
    @Override
    public ResponseDTO deleteWorkforce(Integer scheduleId) {
        Integer doctorId = schedulingMapper.getDoctorByScheduleId(scheduleId);
        ScheduleDO scheduleDO = schedulingMapper.checkScheduling(doctorId);
        if (scheduleDO != null) {
            return ResponseDTO.error(ResponseCode.ERROR, "当前排班已被预约，无法删除");
        }
        Integer count = schedulingMapper.deleteWorkforce(scheduleId);
        String date = notificationMapper.getDateByScheduleId(scheduleId);
        scheduleService.delAppointmentAndNotice(doctorId, scheduleId, date);
        if (count > 0) {
            return ResponseDTO.success();
        }
        return ResponseDTO.error(ResponseCode.ERROR);
    }

    // 修改排班信息
    @Override
    public ResponseDTO updateWorkforce(UpdateSchedulingVO vo) {
        Integer doctorId = vo.getDoctorId();
        ScheduleDO scheduleDO = schedulingMapper.checkScheduling(doctorId);
        if (scheduleDO != null) {
            return ResponseDTO.error(ResponseCode.ERROR, "当前排班已被预约，无法进行修改");
        }
        Integer count = schedulingMapper.checkScheduleUpdate(vo);
        if (count != 0) {
            return ResponseDTO.error(ResponseCode.ERROR, "当前排班与现有排班存在冲突，请重新选择排班时间");
        }
        // 获取原本的日期
        String originalDate = notificationMapper.getOriginalDate(vo.getScheduleId());
        schedulingMapper.updateWorkforce(vo);
        // 修改排班的生成预约号于消息通知状态
        scheduleService.updateSchedule(vo.getDoctorId(), vo.getScheduleId());
        // 删除预约号于消息通知
        System.out.println("日期" + vo.getScheduleDate());

        scheduleService.delAppointmentAndNotice(vo.getDoctorId(), vo.getScheduleId(), originalDate);
        // 重新生成消息通知
        scheduleService.generateNotice();
        // 重新生成预约号
        scheduleService.generateReservationNumber();
        return ResponseDTO.success();
    }

    // 根据科室查询医生
    @Override
    public ResponseDTO getDoctorListByDepartment(Integer departmentId) {
        List<DoctorDO> list = schedulingMapper.getDoctorListByDepartment(departmentId);
        return ResponseDTO.success(list);
    }

    // 新增排班
    @Override
    public ResponseDTO addWorkforce(AddSchedulingVO vo) {
        Integer count = schedulingMapper.checkScheduleConflict(vo);
        if (count != 0) {
            return ResponseDTO.error(ResponseCode.ERROR, "当前排班与现有排班存在冲突，请重新选择排班时间");
        } else {
            schedulingMapper.addWorkforce(vo);
            // 生成消息通知
            scheduleService.generateNotice();
            // 生成预约号
            scheduleService.generateReservationNumber();
            return ResponseDTO.success();
        }
    }
}
